package com.harmontronics.hanma.paas.paas.controller;

import cn.hutool.core.bean.BeanUtil;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import com.harmontronics.hanma.paas.paas.consts.BaseI18nKeyEnum;
import com.harmontronics.hanma.paas.paas.consts.NumberEnum;
import com.harmontronics.hanma.paas.paas.exception.ErrorKeyException;
import com.harmontronics.hanma.paas.paas.token.TenantUserTokenKit;
import com.harmontronics.hanma.paas.paas.token.UserInfo;
import com.harmontronics.hanma.paas.paas.utils.RequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.harmontronics.hanma.paas.paas.service.HmTenantExtService;
import com.harmontronics.hanma.paas.paas.dto.output.HmTenantResult;
import com.harmontronics.hanma.paas.paas.dto.input.HmTenantCreateInput;
import com.harmontronics.hanma.paas.paas.domain.tenant.entity.HmTenant;
import com.harmontronics.hanma.paas.paas.utils.ParamUtils;

import javax.validation.Valid;


/**
 * 租户表
 *
 * @author paas-generator
 * @email paas-generator@harmontronics.com
 * @date 2021-03-18 14:13:32
 */
@RestController
@RequestMapping("/api/tenant")
@Api(tags = {"租户表"})
public class HmTenantController {
    @Autowired
    private HmTenantExtService hmTenantappService;

    @Autowired
    private TenantUserTokenKit tenantUserTokenKit;

    /**
     * 列表
     */
//    @GetMapping("/list")
//    @ApiOperation("列表")
//    public ResponseEntity<List<HmTenantResult>> list() {
//        List<HmTenant> hmTenantList = hmTenantappService.getList();
//
//        return ResponseEntity.ok(Convert.toList(HmTenantResult.class, hmTenantList));
//    }

    /**
    * 分页列表
    */
//    @GetMapping("/page/list")
//    @ApiOperation("分页列表")
//    public ResponseEntity<PageBase<HmTenantResult>> list(@ApiParam(value = "页码", defaultValue = "1") @RequestParam(required = false, defaultValue = "1") int pageNo,
//            @ApiParam(value = "每页数据条数", defaultValue = "20") @RequestParam(required = false, defaultValue = "20") int pageSize) {
//        PageBase<HmTenant> hmTenantPageBase = hmTenantappService.getList(pageNo, pageSize);
//        PageBase<HmTenantResult> resultPageBase = PageBase.copy(hmTenantPageBase,HmTenantResult.class);
//        return ResponseEntity.ok(resultPageBase);
//    }


    /**
     * 根据登录信息获取当前租户信息
     */
    @GetMapping("/info")
    @ApiOperation("详情")
    public ResponseEntity<HmTenantResult> info() {
        UserInfo userInfo = tenantUserTokenKit.getUserInfo();
        HmTenant hmTenant =hmTenantappService.findById(userInfo.getTenantId());
            HmTenantResult result = BeanUtil.toBean(hmTenant,HmTenantResult.class);
        return ResponseEntity.ok(result);
    }

    /**
     * 保存
     */
    @PostMapping()
    @ApiOperation("保存")
    public ResponseEntity<String> save(@Valid @RequestBody HmTenantCreateInput hmTenantInput,BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        UserInfo userInfo = tenantUserTokenKit.getUserInfo();
        //只有管理员可以修改租户信息
        if(!userInfo.isAdmin()){
            throw new ErrorKeyException(BaseI18nKeyEnum.REQUEST_INVALID);
        }
        HmTenant hmTenant =BeanUtil.toBean(hmTenantInput,HmTenant.class);
        hmTenant.setId(userInfo.getTenantId());
        hmTenantappService.save(hmTenant);
        return ResponseEntity.ok();
    }

    /**
     * 修改
     */
//    @PutMapping
//    @ApiOperation("修改")
//    public ResponseEntity<String> update(@Valid @RequestBody HmTenantUpdateInput hmTenantInput, BindingResult bindingResult) {
//        ParamUtils.valid(bindingResult);
//        HmTenant hmTenant =BeanUtil.toBean(hmTenantInput,HmTenant.class);
//            hmTenantappService.update(hmTenant);
//        return ResponseEntity.ok();
//    }

    /**
     * 删除
     */
//    @DeleteMapping()
//    @ApiOperation("删除")
//    public ResponseEntity<String> delete(@RequestBody Long[] ids) {
//            hmTenantappService.remove(Arrays.asList(ids));
//
//        return ResponseEntity.ok();
//    }

}
